const mysql = require('mysql2/promise');

async function main() {
    const args = process.argv.slice(2);
    let name = '';
    let id = 0;
    for (let i = 0; i < args.length; i++) {
        if ((args[i] === '--name' || args[i] === '-n') && args[i + 1]) {
            name = args[i + 1];
        }
        if ((args[i] === '--id' || args[i] === '-i') && args[i + 1]) {
            id = parseInt(args[i + 1], 10);
        }
    }
    if (!name && (!id || Number.isNaN(id))) {
        console.error('用法: node scripts/inspect-character-training.js --name 聚色色 或 --id 2001');
        process.exit(1);
    }

    const config = {
        host: process.env.DB_HOST || '43.143.253.188',
        port: parseInt(process.env.DB_PORT || '3306', 10),
        user: process.env.DB_USER || 'nextjs',
        password: process.env.DB_PASS || 'yEAiRE2jcxTyRBy5',
        database: process.env.DB_NAME || 'nextjs',
        multipleStatements: true,
        timezone: '+08:00'
    };

    let conn;
    try {
        conn = await mysql.createConnection(config);
        console.log('✅ 已连接数据库');

        let rows;
        if (id && !Number.isNaN(id)) {
            [rows] = await conn.execute(
                'SELECT id, name, lvl, exp, trans FROM characters WHERE id = ? LIMIT 1',
                [id]
            );
        } else {
            [rows] = await conn.execute(
                'SELECT id, name, lvl, exp, trans FROM characters WHERE name = ? LIMIT 1',
                [name]
            );
        }
        if (!rows || rows.length === 0) {
            console.log('❌ 未找到该角色');
            return;
        }
        const ch = rows[0];
        console.log('👤 角色信息:', ch);

        const [tr] = await conn.execute(
            'SELECT is_training, training_status, training_hours, pending_exp FROM character_training WHERE character_id = ? LIMIT 1',
            [ch.id]
        );
        console.log('📊 训练记录:', tr && tr[0] ? tr[0] : '无记录');

        await conn.end();
    } catch (e) {
        console.error('❌ 查询失败:', e.message);
        try { if (conn) await conn.end(); } catch (_) { }
        process.exit(1);
    }
}

if (require.main === module) {
    main();
}


